﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Microsoft.Extensions.DependencyInjection;

public static class AspNetCoreMvcServiceCollectionExtensions
{

    /// <summary>
    /// 注册 Mvc 过滤器
    /// </summary>
    /// <typeparam name="TFilter"></typeparam>
    /// <param name="services"></param>
    /// <param name="configure"></param>
    /// <returns></returns>
    public static IServiceCollection AddMvcFilter<TFilter>(this IServiceCollection services, Action<MvcOptions>? configure = default)
        where TFilter : class, IFilterMetadata
    {
        services.Configure<MvcOptions>(options =>
        {
            options.Filters.AddService<TFilter>();

            // 其他额外配置
            configure?.Invoke(options);
        }).AddTransient<TFilter>();

        return services;
    }
}
